package jsEngine;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import org.junit.Test;

public class FirstTest {

	ScriptEngineManager manager = new ScriptEngineManager();
	ScriptEngine engine = manager.getEngineByName("javascript");
	
	@Test
	public void main() throws ScriptException {
		
		engine.eval("var a=3; var b=4; print(a + b);");
		engine.eval("alert('123');"); //报错
		
		engine.eval("[{\"name\":\"tuyou\"}]");
	}
	
	@Test
	public void varTest() throws ScriptException{
		
		engine.put("a", 3);
		engine.put("b", 4);
		Double d = (Double)engine.eval("a + b");
//		Integer d = (Integer)engine.eval("a + b"); //报错，只能用Double接收，Float都不行
		System.out.println(d);
	}
	
	@Test
	public void parseJSON() throws ScriptException{
		
		Object obj = engine.eval("[{\"name\":\"tuyou\"}]");
		System.out.println(obj);
	}
	
	@Test
	public void parseJSON2() throws ScriptException{
		
		Object obj = engine.eval("[{\"1\":\"tuyou\"}]");
		System.out.println(obj);
	}
}
